package vip.liux.contracts.infrastructure.pay.impl;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.util.Assert;
import vip.liux.contracts.infrastructure.pay.PayClient;
import vip.liux.contracts.infrastructure.pay.PayClientConfig;
import vip.liux.contracts.infrastructure.pay.PayClientFactory;
import vip.liux.contracts.infrastructure.pay.impl.alipay.*;
import vip.liux.contracts.infrastructure.pay.impl.mock.MockPayClient;
import vip.liux.contracts.infrastructure.pay.impl.weixin.*;
import vip.liux.contracts.shared.enums.pay.PayChannelCode;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;

import static vip.liux.contracts.shared.consts.GeneralConstants.newInstance;
import static vip.liux.contracts.shared.enums.pay.PayChannelCode.*;


/**
 * 支付客户端的工厂实现类
 *
 * @author 芋道源码
 */
@Slf4j
public class PayClientFactoryImpl implements PayClientFactory {

    /**
     * 支付客户端 Map
     * <p>
     * key：渠道编号
     */
    private final ConcurrentMap<Long, AbstractPayClient<?>> clients = new ConcurrentHashMap<>();

    /**
     * 支付客户端 Class Map
     */
    private final Map<PayChannelCode, Class<? extends AbstractPayClient<?>>> clientClass = new ConcurrentHashMap<>();

    public PayClientFactoryImpl(ObjectProvider<Supplier<Pair<PayChannelCode, Class<? extends AbstractPayClient<?>>>>> provider) {
        // 微信支付客户端
        clientClass.put(WX_PUB, WxPubPayClient.class);
        clientClass.put(WX_LITE, WxLitePayClient.class);
        clientClass.put(WX_APP, WxAppPayClient.class);
        clientClass.put(WX_BAR, WxBarPayClient.class);
        clientClass.put(WX_NATIVE, WxNativePayClient.class);
        clientClass.put(WX_WAP, WxWapPayClient.class);
        // 支付包支付客户端
        clientClass.put(ALIPAY_WAP, AlipayWapPayClient.class);
        clientClass.put(ALIPAY_QR, AlipayQrPayClient.class);
        clientClass.put(ALIPAY_APP, AlipayAppPayClient.class);
        clientClass.put(ALIPAY_PC, AlipayPcPayClient.class);
        clientClass.put(ALIPAY_BAR, AlipayBarPayClient.class);
        // 钱包支付客户端
        // clientClass.put(WALLET, WalletPayClient.class);
        // Mock 支付客户端
        clientClass.put(MOCK, MockPayClient.class);

        provider.forEach(p -> clientClass.put(p.get().getLeft(), p.get().getRight()));
    }

    @Override
    @SuppressWarnings({"rawtypes"})
    public PayClient getPayClient(Long channelId) {
        AbstractPayClient<?> client = clients.get(channelId);
        if (client == null) {
            log.error("[getPayClient][渠道编号({}) 找不到客户端]", channelId);
        }
        return client;
    }

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public <Config extends PayClientConfig> PayClient createOrUpdatePayClient(Long channelId, String channelCode,
                                                                              Config config) {
        AbstractPayClient<Config> client = (AbstractPayClient<Config>) clients.get(channelId);
        if (client == null) {
            client = this.createPayClient(channelId, channelCode, config);
            client.init();
            clients.put(client.getId(), client);
        } else {
            client.refresh(config);
        }
        return client;
    }

    @SuppressWarnings("unchecked")
    private <Config extends PayClientConfig> AbstractPayClient<Config> createPayClient(Long channelId, String channelCode,
                                                                                       Config config) {
        PayChannelCode channelEnum = PayChannelCode.getByCode(channelCode);
        Assert.notNull(channelEnum, String.format("支付渠道(%s) 为空", channelCode));
        Class<?> payClientClass = clientClass.get(channelEnum);
        Assert.notNull(payClientClass, String.format("支付渠道(%s) Class 为空", channelCode));
        return (AbstractPayClient<Config>) newInstance(payClientClass, new Class[]{Long.class, PayClientConfig.class}, new Object[]{channelId, config});
    }

}
